package com.ldatum.crm.action.admin;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.ldatum.crm.domain.Admin;
import com.ldatum.crm.domain.Employee;
import com.ldatum.crm.service.EmployeeService;
import com.ldatum.crm.serviceImpl.EmployeeServiceImpl;
import com.ldatum.crm.util.MD5;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

@ParentPackage("admin")
@Results({ 
	@Result(name = "mainAction", location = "admin!main.action",type = "redirect")
})
public class AdminAction extends BaseAction {
	private static final long serialVersionUID = 1L;
	
	private List<Employee> employeeList = new ArrayList<Employee>();
	
	private EmployeeService employeeService = new EmployeeServiceImpl();
	
	private Employee employee = new Employee();
	
	private String alertMessage;//警告消息
	private String loginname;//登录账号
	private String password;//密码
	
	/**
	 * 
	 * 描述 : 返回登录界面
	 * @return
	 */
	public String login(){
		return "index";
	}
	
	/**
	 * 
	 * 描述 : 实现登录，用户名密码正确则跳转到主界面，否则提示并返回返回到登录界面. <br>
	 * @return
	 * @throws Exception
	 */
	@InputConfig(resultName = "index")
	public String doLogin() throws Exception{
		try{
			if("".equals(loginname) | "".equals(password)){
				alertMessage = "登录名和密码不能为空";
				return "index";
			}
			Employee emp = employeeService.findByLoginname(Employee.class,loginname);
			
			if (emp == null){
				alertMessage = "登录名不存在";
				return "index";
			}			
			if("离职".equals(emp.getStatus())){
				alertMessage = "此员工是离职状态，不能登录!";
				return "index";
			}
			if (MD5.md5(password).equals(emp.getPassword())) {
				setSession(Admin.LOGIN_EMPLOYEE_ID_SESSION_NAME,emp.getId());
				setSession(Admin.LOGIN_EMPLOYEE_USERNAME_COOKIE_NAME,emp.getFullname());
				setSession(Admin.LOGIN_EMPLOYEE_ROLE_NAME,emp.getRole());
				setSession(Admin.LOGIN_EMPLOYEE_PIC_ADDRESS,emp.getHeadPicture());
				return "mainAction";
			}else{
				alertMessage = "密码错误";
				return "index";
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return "main";
	}
	/**
	 * 主界面
	 * @return
	 * @throws Exception
	 */
	@InputConfig(resultName = "main")
	public String main() throws Exception {
		employee = employeeService.findById(Employee.class,(Integer)getSession(Admin.LOGIN_EMPLOYEE_ID_SESSION_NAME));
		System.out.println(employee.getHeadPicture());
		return "main";
	}
	
	public String logout() {
		getRequest().getSession().removeAttribute(Admin.LOGIN_EMPLOYEE_ID);
		return "index";
	}
	
	/**
	 * 描述 : 用户密码忘记了,像注册邮箱发送邮件. <br>
	 * @return
	 */
	public void forgetPasswd(){
		
	}
	
	/**
	 * 描述 : 用户从主界面回到登录界面，并删除用户session. <br>
	 * @return
	 */
	public String  lock(){
		getRequest().getSession().removeAttribute(Admin.LOGIN_EMPLOYEE_ID);
		return "lock";
	}
	
	public List<Employee> getEmployeeList() {
		return employeeList;
	}

	public void setEmployeeList(List<Employee> employeeList) {
		this.employeeList = employeeList;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public String getAlertMessage() {
		return alertMessage;
	}

	public void setAlertMessage(String alertMessage) {
		this.alertMessage = alertMessage;
	}

	public String getLoginname() {
		return loginname;
	}

	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}	
}
